﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Entity;

namespace BLL
{
    public class TeacherVo
    {
        public string TeacherId { get; set; }
        public string TeacherName { get; set; }
        public string TeacherPassword { get; set; }
        public string TeacherGender { get; set; }
        public string TeacherPhone { get; set; }
        public string TeacherEmail { get; set; }
        public string TeacherAcademyId { get; set; }
        public int TeacherManager { get; set; }
        public List<RoleVo> Roles { get; set; }
        private ModuleDao moduleDao = new ModuleDao();
        private TeachersDAL teacherDAL = new TeachersDAL();
        private UserRolesDAL userRoleDAL = new UserRolesDAL();

        public TeacherVo()
        {
            TeacherName = "";
            TeacherPassword = "";
            TeacherGender = "0";
            TeacherPhone = "";
            TeacherEmail = "";
            TeacherAcademyId = "";
            TeacherManager = 0;
            Roles = new List<RoleVo>();
        }

        public ModuleVo getAuthorizedModules()
        {
            ModuleVo moduleVo = new ModuleVo();
            moduleVo.ModuleID = null;
            moduleVo.MOduleName = "root";
            moduleVo.DisplayName = "竞赛管理系统";
            moduleVo.ModuleURL = "#";
            moduleVo.Children = getAuthorizedChildren(moduleVo);
            return moduleVo;
        }

        protected List<ModuleVo> getAuthorizedChildren(ModuleVo parent)
        {
            List<ModuleVo> children = new List<ModuleVo>();
            foreach (RoleVo role in Roles)
            {
                List<BoModules> modules = moduleDao.GetRoleModules(role.RoleId, parent.ModuleID);
                foreach (BoModules module in modules)
                {
                    bool isExist = false;
                    foreach (ModuleVo mv in children)
                    {
                        if (mv.ModuleID == module.MOD_ID)
                        {
                            isExist = true;
                            break;
                        }
                    }
                    if (isExist)
                        continue;

                    ModuleVo child = new ModuleVo();
                    child.ModuleID = module.MOD_ID;
                    child.MOduleName = module.MOD_NAME;
                    child.DisplayName = module.MOD_DISPLAYNAME;
                    child.ModuleURL = module.MOD_URL;
                    child.ParentModule = parent;
                    child.Children = getAuthorizedChildren(child);
                    children.Add(child);
                }
            }
            return children;
        }

        public void SaveRoles()
        {
            userRoleDAL.ClearTeacherRoles(TeacherId);
            foreach (RoleVo rv in Roles)
            {
                BoUserRoles userRole = new BoUserRoles();
                userRole.USE_TEACHERS = TeacherId;
                userRole.USE_ROLES = rv.RoleId;
                userRoleDAL.Insert(userRole);
            }
        }
    }
}
